﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

using log4net;
using log4net.Config;

namespace kindly
{
    public class Global : System.Web.HttpApplication
    {
        private static List<string> _smtpAccounts = null;
        private static string _currentSmtpAccount = null;

        public static List<string> SmtpAccounts
        {
            get 
            {
                if (_smtpAccounts == null)
                { 
                    _smtpAccounts = new List<string>();
                    // convert string collection into List<string>
                    foreach (string account in kindly.Properties.Settings.Default.smtp_username)
                    {
                        _smtpAccounts.Add(account);
                    }

                }
                return Global._smtpAccounts; 
            }
        }

        public static string CurrentSmtpAccount
        {
            get
            {
                if (string.IsNullOrEmpty(_currentSmtpAccount))
                {
                    _currentSmtpAccount = SmtpAccounts[0]; // get first in array
                }
                return _currentSmtpAccount;                
            }
            set
            {
                _currentSmtpAccount = value;
            }
        }

        private static readonly ILog log = LogManager.GetLogger(typeof(Global));

        protected void Application_Start(object sender, EventArgs e)
        {
            // initialize log4net
            XmlConfigurator.Configure(); // read log4net config
            log.Info("Starting kindly email relay");
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {            
        }

        protected void Application_Error(object sender, EventArgs e)
        {
        }

        protected void Application_End(object sender, EventArgs e)
        {
            log.Info("Ending kindly email relay");
        }
    }
}